package com.example.keli.ui.home.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.keli.R;
import com.example.keli.api.HttpUtils;
import com.example.keli.api.RetrofitManager;
import com.example.keli.api.http.api.BaseResponse;
import com.example.keli.api.http.api.subscriber.SubscriberListener;
import com.example.keli.base.BaseFragment;
import com.example.keli.base.utils.ToastUtil;
import com.example.keli.base.widget.SexSelectDialog;
import com.example.keli.ui.area.AreaActivity;
import com.example.keli.ui.area.AreaVO;
import com.example.keli.ui.center.ActivieList;
import com.example.keli.ui.home.activity.TabMoreActivity;
import com.example.keli.ui.home.model.SexVo;
import com.example.keli.ui.home.model.TabPosition;
import com.example.keli.ui.search.SearchActivity;
import com.flyco.tablayout.SlidingTabLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * description $desc$
 * created by jerry on 2019/3/18.
 */
public class HomeFragment extends BaseFragment {

    @BindView(R.id.address)
    TextView mAddress;
    @BindView(R.id.search_edit)
    EditText mSearchEdit;
    @BindView(R.id.tab_layout)
    SlidingTabLayout mTabLayout;
    @BindView(R.id.viewpage)
    ViewPager mViewpage;
    @BindView(R.id.iv_female)
    ImageView ivFemale;
    @BindView(R.id.tv_female)
    TextView tvFemale;
    @BindView(R.id.iv_man)
    ImageView ivMan;
    @BindView(R.id.tv_man)
    TextView tvMan;
    @BindView(R.id.tab_more)
    ImageView tabMore;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.ll_search)
    LinearLayout llSearch;
    @BindView(R.id.sex_select)
    ImageView Selectsex;
    @BindView(R.id.search_end)
    ImageView Endsearch;

    private List<Fragment> mTabContents = new ArrayList<>();
    public static final String FRAGMENT_TYPE_ID = "FRAGMENT_TYPE";
    protected final ArrayList<String> sTitle = new ArrayList<>();
    private String region_id = "";
    private String address = "全国";
    private int index = 0;//0女，1男，2都想看

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onChangedCity(AreaVO event) {
        if (null != event) {
            if (TextUtils.equals(event.getName(), "全国")) {
                region_id = "";
            } else {
                region_id = String.valueOf(event.getId());
            }
            address = event.getName();
            mAddress.setText(event.getName());
            toolbarTitle.setText(address);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onChangedTab(TabPosition tabPosition) {
        mTabLayout.setCurrentTab(tabPosition.getPosition() + 1);
    }

    @Override
    protected int layoutId() {
        return R.layout.fragment_home;
    }

    MyPagerAdapter adapter;

    @Override
    protected void bindData() {
        EventBus.getDefault().register(this);
        sTitle.add("热门");
        RecommendFragment recommendFragment = new RecommendFragment();
        Bundle bundle = new Bundle();
        bundle.putString(FRAGMENT_TYPE_ID, region_id);
        recommendFragment.setArguments(bundle);
        mTabContents.add(recommendFragment);
        ivFemale.setSelected(true);
        mAddress.setText(address);
        startProgressDialog();
        loadTitleDatas();

    }

    @Override
    protected void bindListener() {
    }

    @OnClick({R.id.iv_female, R.id.iv_man, R.id.tab_more, R.id.toolbar_title, R.id.sex_select, R.id.search_end, R.id.address, R.id.ll_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_female:
                ivFemale.setSelected(true);
                ivMan.setSelected(false);
                tvFemale.setText("看小姐姐");
                tvMan.setText("");
                EventBus.getDefault().post(new SexVo("2"));
                break;
            case R.id.iv_man:
                ivFemale.setSelected(false);
                ivMan.setSelected(true);
                tvFemale.setText("");
                tvMan.setText("看小哥哥");
                EventBus.getDefault().post(new SexVo("1"));
                break;
            case R.id.tab_more:
                TabMoreActivity.start(getActivity(), sTitle);
                break;
            case R.id.toolbar_title:
                toActivity(AreaActivity.class);
                break;
            case R.id.sex_select:
                new SexSelectDialog(getActivity(), index, new SexSelectDialog.SexListener() {
                    @Override
                    public void callSex(int sex) {
                        index = sex;
                        switch (sex) {
                            case 0:
                                Selectsex.setImageResource(R.drawable.ic_female24);
                                EventBus.getDefault().post(new SexVo("2"));
                                break;
                            case 1:
                                Selectsex.setImageResource(R.drawable.ic_choosefemale);
                                EventBus.getDefault().post(new SexVo("1"));
                                break;
                            case 2:
                                Selectsex.setImageResource(R.drawable.ic_chooseall24);
                                break;
                        }
                    }
                }).show();
                break;
            case R.id.search_end:
                toActivity(SearchActivity.class);
                break;

            case R.id.ll_search:
                break;
            case R.id.address:
                toActivity(AreaActivity.class);
                break;
        }
    }

    private class MyPagerAdapter extends FragmentStatePagerAdapter {
        List<Fragment> mTabContents;
        List<String> sTitle;

        public MyPagerAdapter(FragmentManager fm, List<Fragment> mTabContents, List<String> sTitle) {
            super(fm);
            this.mTabContents = mTabContents;
            this.sTitle = sTitle;
        }

        @Override
        public Fragment getItem(int position) {
            return mTabContents.get(position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        }

        @Override
        public int getCount() {
            return mTabContents.size();
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return sTitle.get(position);
        }

    }

    private void loadTitleDatas() {
        Map<String, String> map = new HashMap<>();
//        map.put("region_id", region_id);
        map.put("page", "1");
        HttpUtils.ApiFunc(RetrofitManager.mApiService.getActivity(map), new SubscriberListener<BaseResponse<ActivieList>>() {
            @Override
            public void onSuccess(BaseResponse<ActivieList> response) {
                for (int i = 0; i < response.getData().getData().size(); i++) {
                    sTitle.add(response.getData().getData().get(i).getTitle());
                    HomeListFragment homeListFragment = new HomeListFragment();
                    Bundle bundle = new Bundle();
                    bundle.putString(FRAGMENT_TYPE_ID, String.valueOf(response.getData().getData().get(i).getId()));
                    homeListFragment.setArguments(bundle);
                    mTabContents.add(homeListFragment);
                }
                adapter = new MyPagerAdapter(getFragmentManager(), mTabContents, sTitle);
                mViewpage.setAdapter(adapter);
                mTabLayout.setViewPager(mViewpage);

                stopProgressDialog();
            }

            @Override
            public void onFailer(String msg) {
                stopProgressDialog();
                ToastUtil.show(msg);
            }

            @Override
            public void onTokenError() {

            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
